package com.tfjybj.itoo.exam.ext.service;

import com.dmsdbj.itoo.tool.tojson.JacksonJsonUntil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.tfjybj.itoo.exam.entity.QuestionTypeEntity;
import com.tfjybj.itoo.exam.ext.mongo.QuestionTemplate;
import com.tfjybj.itoo.exam.ext.repository.QuestionTemplateRepository;
import com.tfjybj.itoo.exam.model.*;
import com.tfjybj.itoo.exam.provider.dao.QuestionTypeDao;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 商俊帅
 * @date 2022/01/14 15:32
 **/
@Service("7a3b6be6-f086-4af9-bad1-73ebcd69ae9d")
@Slf4j
public class TemplateServiceImpl implements TemplateService{

    private  Map<String, QuestionTypeModel> id2Entity=new HashMap<>();

    @Autowired
    private QuestionTypeDao questionTypeDao;
    @Autowired
    private QuestionTemplateRepository templateRepository;

    @PostConstruct
    public void set(){
        List<QuestionTypeModel> questionTypeModels = questionTypeDao.queryAllQuestionType();
        Map<String, QuestionTypeModel> collect = questionTypeModels.stream()
                .collect(Collectors.groupingBy(QuestionTypeModel::getId,Collectors.collectingAndThen(Collectors.toList(),value->value.get(0))));
        id2Entity=collect;
        log.info("question type already init");
    }

    @Override
    public TemplateQuestionTypeDetailModel getTemplateById(String templateId){
        TemplateQuestionTypeDetailModel templateDetailInfo = new TemplateQuestionTypeDetailModel();
        Optional<QuestionTemplate> templateOptional = templateRepository.findById(templateId);
        if (!templateOptional.isPresent()){
            return null;
        }

        QuestionTemplate questionTemplate = templateOptional.get();
        List<QuestionTemplate.QuestionTemplateDetail> questionTemplateDetail = questionTemplate.getQuestionTemplateDetail();
        List<TemplateDetailsModel> templateDetailsModels = toList(questionTemplateDetail);
        TemplateModel templateModel = template2TemplateModel(questionTemplate);

        templateDetailInfo.setTemplateDetailsModelList(templateDetailsModels);
        templateDetailInfo.setTemplateModel(templateModel);
        return templateDetailInfo;
    }

    private List<TemplateDetailsModel> toList(List<QuestionTemplate.QuestionTemplateDetail> questionTemplateDetails){
        List<TemplateDetailsModel> models = new ArrayList<>();
        for (QuestionTemplate.QuestionTemplateDetail detail: questionTemplateDetails){
            TemplateDetailsModel templateDetailsModel = detailsModel(detail);
            List<QuestionTemplate.QuestionTemplateExtract> questionTemplateExtracts = detail.getQuestionTemplateExtracts();
            if (!CollectionUtils.isEmpty(questionTemplateExtracts)){
                List<TemplateDetailsExtractModel> extractModels = new ArrayList<>();
                for (QuestionTemplate.QuestionTemplateExtract extract : questionTemplateExtracts){
                    TemplateDetailsExtractModel model = extractModel(extract);
                    extractModels.add(model);
                }
                templateDetailsModel.setTemplateDetailsExtractModel(extractModels);
            }
            List<QuestionTemplate.QuestionTemplateCustom> questionTemplateCustoms = detail.getQuestionTemplateCustoms();
            if (!CollectionUtils.isEmpty(questionTemplateCustoms)){
                List<TemplateDetailsCustomModel> customModels = new ArrayList<>();
                for (QuestionTemplate.QuestionTemplateCustom custom : questionTemplateCustoms){
                    TemplateDetailsCustomModel model = customModel(custom);
                    customModels.add(model);
                }
                templateDetailsModel.setTemplateDetailsCustomModel(customModels);
            }
            models.add(templateDetailsModel);
        }
        return models;
    }

    private TemplateModel template2TemplateModel(QuestionTemplate questionTemplate){
        TemplateModel templateModel = new TemplateModel();
        templateModel.setId(questionTemplate.getId());
        templateModel.setName(questionTemplate.getName());
        templateModel.setCourseId(questionTemplate.getCourseId());
        //这里没有课程name
        templateModel.setStatus(questionTemplate.getStatus());
        templateModel.setScore(questionTemplate.getScore());
        templateModel.setTotalNumber(questionTemplate.getTotalNumber());
        templateModel.setOrganizingType(questionTemplate.getOrganizingType());
        //没有组卷方式
        templateModel.setDescription(questionTemplate.getDescription());
        return templateModel;
    }

    private TemplateDetailsModel detailsModel(QuestionTemplate.QuestionTemplateDetail detail){
        TemplateDetailsModel model = new TemplateDetailsModel();
        model.setId(detail.getId());
        model.setTemplateId(detail.getTemplateId());
        model.setQuestionTypeId(detail.getQuestionTypeId());
        model.setQuestionClassifyId(detail.getQuestionClassifyId());
        model.setQuestionName(detail.getQuestionName());
        model.setQuestionNumber(detail.getQuestionNumber());
        model.setQuestionScore(String.valueOf(detail.getQuestionScore()));
        model.setSmallQuestionScore(String.valueOf(detail.getSmallQuestionScore()));
        model.setIsExtract(detail.getIsExtract());
        model.setIsCustom(detail.getIsCustom());
        model.setDegree(detail.getDegree());
        model.setMessingPoints(detail.getMessingPoints());
        model.setQuestionTypeDescribe(detail.getQuestionTypeDescribe());
        model.setIsBlanks(detail.getIsBlanks());
        //questiontypecode  需要从数据库查
        model.setQuestionTypeCode(id2Entity.get(detail.getQuestionTypeId()).getQuestionCode());
        model.setQuestionTypeOrder(detail.getQuestionTypeOrder());
        model.setQuestionCode(Integer.valueOf(id2Entity.get(detail.getQuestionTypeId()).getQuestionCode()));
        model.setQuestionClassifyId(detail.getQuestionClassifyId());
        return model;
    }

    private TemplateDetailsExtractModel extractModel(QuestionTemplate.QuestionTemplateExtract extract){
        TemplateDetailsExtractModel model = new TemplateDetailsExtractModel();
        model.setId(extract.getId());
        model.setQuestionId(extract.getQuestionId());
        model.setTemplateDetailsId(extract.getTemplateDetailsId());
        model.setOperator(extract.getOperator());
        return model;
    }

    private TemplateDetailsCustomModel customModel(QuestionTemplate.QuestionTemplateCustom custom){
        return TemplateDetailsCustomModel.builder()
                .id(custom.getId())
                .questionClassifyId(custom.getQuestionClassifyId())
                .questionNumber(custom.getQuestionNumber())
                .degree(custom.getDegree())
                .questionScore(String.valueOf(custom.getQuestionScore()))
                .templateDetailsId(custom.getTemplateDetailsId())
                .isSenior(custom.getIsSenior()).build();
    }
}
